#ifndef _ALLOC_h
#define _ALLOC_h

#ifdef	__cplusplus
extern "C" {
#endif

#include <crt_util.h>

#define ngx_memzero(buf, n)       bzero(buf,n)
#define ngx_memset(buf, c, n)     (void) s_memset(buf, c, n)
#define ngx_memcpy(dst, src, n)   (void) s_memcpy(dst, src, n)

//typedef unsigned int		uintptr_t;
//typedef unsigned int        size_t;

#define ngx_free          free
#define ngx_memalign(alignment, size)  ngx_alloc(size)

#define NGX_MAX_ALLOC_FROM_POOL  4097

#define NGX_DEFAULT_POOL_SIZE    (16 * 1024)

#define NGX_POOL_ALIGNMENT       16
#define NGX_MIN_POOL_SIZE                                                     \
    ngx_align((sizeof(ngx_pool_t) + 2 * sizeof(ngx_pool_large_t)),            \
              NGX_POOL_ALIGNMENT)

#ifndef NGX_ALIGNMENT
#define NGX_ALIGNMENT   sizeof(unsigned long)    /* platform word */
#endif

#define ngx_align(d, a)     (((d) + (a - 1)) & ~(a - 1))
#define ngx_align_ptr(p, a)                                                   \
    (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))


typedef void(*ngx_pool_cleanup_pt)(void *data);

typedef struct ngx_pool_s        ngx_pool_t;
typedef struct ngx_chain_s       ngx_chain_t;
typedef struct ngx_pool_large_s  ngx_pool_large_t;
typedef struct ngx_pool_cleanup_s  ngx_pool_cleanup_t;

typedef unsigned char		u_char;
typedef unsigned int		ngx_uint_t;
typedef int					ngx_int_t;
typedef void *				ngx_buf_tag_t;
#define  NGX_OK          0
#define  NGX_ERROR      -1
#define  NGX_AGAIN      -2
#define  NGX_BUSY       -3
#define  NGX_DONE       -4
#define  NGX_DECLINED   -5
#define  NGX_ABORT      -6
typedef struct ngx_buf_s  ngx_buf_t;

struct ngx_buf_s {
	u_char				*pos;
	u_char				*last;
	off_t				file_pos;
	off_t				file_last;

	u_char				*start;         /* start of buffer */
	u_char				*end;           /* end of buffer */
	ngx_buf_tag_t		tag;
	ngx_buf_t			*shadow;
	unsigned			temporary : 1;
	unsigned			memory : 1;
	unsigned			 mmap : 1;

	unsigned			recycled : 1;
	unsigned			in_file : 1;
	unsigned			flush : 1;
	unsigned			sync : 1;
	unsigned			last_buf : 1;
	unsigned			last_in_chain : 1;

	unsigned			last_shadow : 1;
	unsigned			temp_file : 1;

	int					num;
};


struct ngx_chain_s {
	ngx_buf_t    *buf;
	ngx_chain_t  *next;
};

struct ngx_pool_cleanup_s {
	ngx_pool_cleanup_pt   handler;
	void                 *data;
	ngx_pool_cleanup_t   *next;
};

struct ngx_pool_large_s {
	ngx_pool_large_t     *next;
	void                 *alloc;
};


typedef struct {
	u_char               *last;
	u_char               *end;
	ngx_pool_t           *next;
	ngx_uint_t            failed;
} ngx_pool_data_t;


struct ngx_pool_s {
	ngx_pool_data_t       d;
	size_t                max;
	ngx_pool_t           *current;
	ngx_chain_t          *chain;
	ngx_pool_large_t     *large;
	ngx_pool_cleanup_t   *cleanup;
};



void *ngx_alloc(size_t size);
void *ngx_calloc(size_t size);

ngx_pool_t *ngx_create_pool(size_t size);
void ngx_destroy_pool(ngx_pool_t *pool);
void ngx_reset_pool(ngx_pool_t *pool);

void *ngx_palloc(ngx_pool_t *pool, size_t size);
void *ngx_pnalloc(ngx_pool_t *pool, size_t size);
void *ngx_pcalloc(ngx_pool_t *pool, size_t size);
void *ngx_pmemalign(ngx_pool_t *pool, size_t size, size_t alignment);
ngx_int_t ngx_pfree(ngx_pool_t *pool, void *p);




#ifdef	__cplusplus
}
#endif


#endif   

